package team.you.change.service.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.you.change.dao.CollegeMapper;
import team.you.change.pojo.College;
import team.you.change.util.Result;
import team.you.change.service.CollegeService;
import team.you.change.util.YuoUtil;

import java.util.List;

@Service
public class CollegeServiceImpl implements CollegeService {
    @Autowired
    private CollegeMapper collegeMapper;
    @Override
    public Result getAllColleges(Integer page, Integer limit) {
        Result result = new Result();
        List<College> colleges = collegeMapper.selectAll(YuoUtil.getPage(page, limit), limit);
        Integer count = collegeMapper.getCount();
        return YuoUtil.setData(result, colleges, count);
    }

    @Override
    public Result getAllCollegeId() {
        Result result = new Result();
        List<College> colleges = collegeMapper.selectAllCollId();
        if(colleges.size() <= 0){
            return YuoUtil.setFlaseMsg(result);
        }else {
            result.setData(colleges);
            return result;
        }
    }

    @Override
    public Result add(String name, Integer type, Integer college) {
        Result result = new Result();
        College coll = new College();
        coll.setName(name);
        coll.setType(type);
        coll.setCollege(college);
        try{
            return YuoUtil.crudMsg(collegeMapper.insert(coll), result);
        }catch (Exception e){
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result getAllPId() {
        Result result = new Result();
        List<College> college = collegeMapper.selectAllCollId();
        if(college != null){
            result.setData(college);
            return result;
        }else{
            return YuoUtil.setFlaseMsg(result);
        }
    }

    @Override
    public Result del(Integer id) {
        Result result = new Result();
        try {
            College college = collegeMapper.selectByPrimaryKey(id);
            if(college != null){
                return YuoUtil.crudMsg(collegeMapper.deleteByPrimaryKey(id), result);
            }else {
                return YuoUtil.setFlaseMsg(result);
            }
        }catch (Exception e){
            return YuoUtil.setErrorMsg(e,result);
        }
    }

    @Override
    @Transactional //添加事务
    public Result delete(String[] ids) {
        Result result = new Result();
        try {
            for (int i = 0; i < ids.length; i++) {
                collegeMapper.deleteByPrimaryKey(Integer.parseInt(ids[i]));
            }
            return result;
        } catch (Exception e) {
            return YuoUtil.setErrorMsg(e, result);
        }
    }
    @Override
    public Result update(College college) {
        Result result = new Result();
        try {
            if(college.getType() == 0){
                college.setCollege(0);
            }
            return YuoUtil.crudMsg(collegeMapper.updateByPrimaryKey(college), result);
        } catch (Exception e) {
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result selectByLike(String value, Integer type, Integer page, Integer limit) {
        Result result = new Result();
        List<College> colleges = collegeMapper.selectByLike(value, type, YuoUtil.getPage(page, limit), limit); //模糊查询时分页
        List<College> count = collegeMapper.selectByLike(value, type, null, null);
        return YuoUtil.setData(result, colleges, count.size());
    }

    @Override
    public Result getCollegeOption() {
        Result result = new Result();
        List<College> college = collegeMapper.selectAllCollId();
        if(college.size() > 0){
            result.setData(college);
            return result;
        }else{
            return YuoUtil.setFlaseMsg(result);
        }
    }

    @Override
    public Result getMajorOption(Integer id) {
        Result result = new Result();
        List<College> colleges = null;
        if(id == null){
            colleges = collegeMapper.selectAllMajor();
            if(colleges.size() > 0){
                result.setData(colleges);
                return result;
            }else{
                return YuoUtil.setFlaseMsg(result);
            }
        }else {
            colleges = collegeMapper.selectAllMajorId(id);
            if(colleges.size() > 0){
                result.setData(colleges);
                return result;
            }else{
                result.setCode(500);
                result.setMsg("暂无下属专业！");
                return result;
            }
        }
    }
}
